from flask import Blueprint,jsonify,g
from flask_restful import Api,Resource,reqparse,fields,marshal
from syl_project.users import user_fields
from syl_project.course import course_fields
from common.utils.login_auth import login_auth
from common.models.course import *
from common.models.users import *
import json

comment_fields = {
    'cid':fields.Integer,
    'content':fields.String,
    'created_time':fields.DateTime,
    'like_count':fields.Integer,
    'reply_count':fields.Integer,
    'user_id':fields.Integer,
    'to_user':fields.Integer,
    'parent_id':fields.Integer,
    'course_id':fields.Integer
}

comment_bp = Blueprint('comment_bp',__name__,url_prefix='/comment')

api = Api(comment_bp)

class Commentall(Resource): # 根据课程获取评论
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('course_id')
        args = parser.parse_args()
        course_id = args['course_id']
        # course = Course.query.filter_by(id=course_id).first()
        # comments = course.comments
        # result = json.loads(json.dumps(marshal(comments,comment_fields)))
        # for i in result:
        #     user = UserModel.query.filter_by(uid=i['user_id']).first()
        #     i['user_info'] = {
        #         'id': user.uid,
        #         'username': user.uname,
        #         'img': user.profile_photo,
        #         'vid': 1,
        #         'experience':10,
        #     }
        #     print(i['parent_id'])
        #     if i['parent_id']!=0:
        #         a = Comment.query.filter_by(cid=i['parent_id']).first()
        #         childlist = marshal(a, comment_fields)
        #         print(childlist)
        #         user2 = UserModel.query.filter_by(uid=childlist['user_id']).first()
        #         childlist['user_info'] = {
        #             'id': user2.uid,
        #             'username': user2.uname,
        #             'img': user2.profile_photo,
        #             'vid': 1,
        #             'experience': 10,
        #         }
        #         i['childlist'] = childlist
        #     else:
        #         i['childlist'] = []
        #
        # return jsonify(code=200,msg='获取评论成功',data=result)
        course = Course.query.filter_by(id=course_id).first()
        comments = course.comments
        result = json.loads(json.dumps(marshal(comments,comment_fields)))
        for i in result:
            user = UserModel.query.filter_by(uid=i['user_id']).first()
            i['user_info'] = {
                'id': user.uid,
                'username': user.uname,
                'img': user.profile_photo,
                'vid': 1,
                'experience':10,
            }
            coms = Comment.query.filter_by(parent_id=i['cid']).all()
            if coms:
                comss = marshal(coms,comment_fields)
                for j in comss:
                    user = UserModel.query.filter_by(uid=j['user_id']).first()
                    j['user_info'] = {
                        'id': user.uid,
                        'username': user.uname,
                        'img': user.profile_photo,
                        'vid': 1,
                        'experience': 10,
                    }
                i['childlist'] = comss
            else:
                i['childlist'] = []
        return jsonify(code=200,msg='获取评论成功',data=result)



class CommentAdd(Resource):
    @login_auth
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('cid')
        parser.add_argument('ques_id')
        parser.add_argument('content')
        args = parser.parse_args()
        cid = args['cid']
        ques_id = args['ques_id']
        content = args['content']
        uid = g.uid
        if ques_id == '0':
            comment = Comment(course_id=cid,user_id=uid,content=content)
            db.session.add(comment)
            db.session.commit()
            return jsonify(code=200,msg='添加评论成功')
        else:
            comment = Comment(course_id=cid, parent_id=ques_id,user_id=uid, content=content)
            db.session.add(comment)
            db.session.commit()
            return jsonify(code=200, msg='添加评论成功')





api.add_resource(Commentall,'/get_comment')
api.add_resource(CommentAdd,'/add_comment')